var VIE = {
  pageSize:10,
  showToast: function (msg, callback) {
    $("#i_toast_mask").css("display", "block");
    $("#i_toast_content").css("display", "block");
    $("#i_toast_content").text(msg);
    setTimeout(function () {
      $("#i_toast_mask").css("display", "none");
      $("#i_toast_content").css("display", "none");
      if (callback && Object.prototype.toString.call(callback) === '[object Function]')
        callback.apply();
    }, 1500);
  },
  showToastKeep: function (msg, callback) {
    $("#i_toast_mask").css("display", "block");
    $("#i_toast_content_msg").css("display", "block");
    $("#i_toast_content_msg").html(msg);
  },
  hideToastKeep: function () {
    $("#i_toast_mask").css("display", "none");
    $("#i_toast_content_msg").css("display", "none");
  },
  getJ:function(url,param, ctx, successFun, errorFun, async){
    this.ajax({type:'GET',url: url, param:param, ctx:ctx, success:successFun, error:errorFun,async:async});
  },
  postJ:function(url,param, ctx, successFun, errorFun, async){
    this.ajax({type:'POST',url: url, param:param, ctx:ctx, success:successFun, error:errorFun,async:async});
  },
  putJ: function(url,param, ctx, successFun, errorFun, async){
    this.ajax({type:'PUT',url: url, param:param, ctx:ctx, success:successFun, error:errorFun,async:async});
  },
  deleteJ: function(url,param, ctx, successFun, errorFun, async){
    this.ajax({type:'DELETE',url: url, param:param, ctx:ctx, success:successFun, error:errorFun,async:async});
  },
  upload: function(name,url, ctx,success,error){
    var _this = this;
    var oData = new FormData(document.forms.namedItem(name));
    var oReq = new XMLHttpRequest();
    oReq.open( "POST", url , true );
    oReq.onload = function(oEvent) {
      var data = JSON.parse(oReq.responseText);
      if(oReq.status == 200){
        if (success && Object.prototype.toString.call(success) === '[object Function]')
          success.apply(ctx, [data]);
      }else{
        if (oReq.status === 401) {
          _this.error();
        }
        if (error && Object.prototype.toString.call(error) === '[object Function]')
          error.apply(ctx, [oReq]);
      }
    };
    oReq.send(oData);
  },
  //根据json以及token发送ajax
  ajax:function(obj){
    var resObj = this.setArguments(obj);
    //this.limit();
    $.ajax({
      type: resObj.type,
      url: resObj.url,
      data: resObj.param,
      timeout: 60000,
      async: resObj.async,
      contentType: resObj.contentType,
      beforeSend: function(xhr, settings) {
        //xhr.setRequestHeader("Authorization", "Bearer " + tokenInfo.access_token);
      },
      success: function(data) {
        if (resObj.success && Object.prototype.toString.call(resObj.success) === '[object Function]')
          resObj.success.apply(resObj.ctx, [data]);
      },
      error: function(xhr, type) {
        if (xhr.status === 401) {
          this.error();
        }
        if (resObj.error && Object.prototype.toString.call(resObj.error) === '[object Function]')
          resObj.error.apply(resObj.ctx, [xhr, type]);
      }
    });
  },
  //判断传输类型
  setArguments: function(obj){
    var resObj = obj;
    resObj.contentType = "application/x-www-form-urlencoded; charset=utf-8";
    if (resObj.type === "POST" || resObj.type === "PUT") {
      resObj.contentType = "application/json;";
      resObj.param = JSON.stringify(resObj.param);
    }
    if(resObj.async == undefined){
      resObj.async = true;
    }
    return resObj;
  },
  //重复获取登录权
  limit: function(){
    var tokenInfo = JSON.parse(localStorage.getItem("secret"));
    tokenInfo = tokenInfo.length > 1 ? tokenInfo : {tokenInfo:''};
    if (!tokenInfo) {
      this.error();
    }
    if (new Date().getTime() - tokenInfo.expires_time > 0 ) {
      var param = {refresh_token:tokenInfo.refresh_token+ window.secretID,grant_type:'refresh_token'};
      this.get( window.frontLogonHost + '/oauth/oauth2/token',
        param, this.resetExpiresTime, this.error);
    }
  },
  //设置登录权
  resetExpiresTime:function(data){
    data.expires_time = new Date().getTime() + data.expires_in * 1000;
    localStorage.setItem('secret', JSON.stringify(data));
  },
  error: function(){
    //window.location = 'login.html';
  },
  //非限制获取
  get:function(url,param,successfun,errorfun){
    $.ajax({
      type: 'GET',
      url: url,
      data:param,
      async: true,
      contentType: "application/json;",
      success: successfun,
      error: errorfun
    });
  },
  getSearchString: function(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null)
      return decodeURIComponent(r[2]);
    return "";
  },
  QueryString : function(key) {
  var uri = decodeURIComponent(window.location.href);
  var re = new RegExp("[\&\?]" + key + "\=([^\&]*)", "ig");
  return ((uri.match(re)) ? (uri.match(re)[0].substr(key.length + 2)) : null);
}
}
module.exports = VIE;